package com.jingtao.rabbitmq.consumer;

import com.jingtao.rabbitmq.constant.RabbitMQConstants;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * HelloWorld队列消息消费者
 */
@Component
@Log4j2
public class HelloWorldQueueConfig {

    /**
     * 初始化HelloWorld队列
     *
     * @return 队列实例
     */
    @Bean
    public Queue initQueue() {
        // 创建名为HELLO_WORLD_QUEUE_NAME的持久化队列
        return new Queue(RabbitMQConstants.HELLO_WORLD_QUEUE_NAME, true);
    }

    /**
     * 监听HelloWorld队列，接收并处理消息
     *
     * @param msg 接收到的消息内容
     */
    @RabbitListener(queues = {RabbitMQConstants.HELLO_WORLD_QUEUE_NAME})
    public void helloWorldQueueConfig(String msg) {
        log.info("消费者接收到消息，队列名称：{}，消息的内容：{}", RabbitMQConstants.HELLO_WORLD_QUEUE_NAME, msg);
    }
}
